package demo25;


import java.util.concurrent.locks.ReentrantLock;

/*ReentrantLock可以指定为公平锁，构造方法中将fair属性设置为true即为公平锁，fair默认为false*/
public class T extends Thread {
    private static ReentrantLock lock = new ReentrantLock(); //参数为true表示为公平锁，可对比输出结果

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "-获得锁 ");
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        T t = new T();
        Thread t1 = new Thread(t);
        Thread t2 = new Thread(t);
        t1.start();
        t2.start();
    }
}